package com.ruoyi.project.channel.channelAccount.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.channel.channelAccountGroup.domain.ChannelAccountGroup;
import com.ruoyi.project.channel.channelAccountGroup.service.IChannelAccountGroupService;
import com.ruoyi.project.channel.channelOrder.domain.ChannelOrder;
import com.ruoyi.project.channel.payerPayType.domain.PayerPayType;
import com.ruoyi.project.channel.payerPayType.service.IPayerPayTypeService;
import com.ruoyi.project.pay.PayConfig;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.businessInfo.domain.BusinessInfo;
import com.ruoyi.project.channel.businessInfo.service.IBusinessInfoService;
import com.ruoyi.project.channel.channelAccount.domain.ChannelAccount;
import com.ruoyi.project.channel.channelAccount.service.IChannelAccountService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 通道账户信息操作处理
 *
 * @author ruoyi
 * @date 2019-04-09
 */
@Controller
@RequestMapping("/channel/channelAccount")
public class ChannelAccountController extends BaseController {
    private String prefix = "channel/channelAccount";

    @Autowired
    private IChannelAccountService channelAccountService;
    @Autowired
    private IBusinessInfoService businessInfoService;
    @Autowired
    private IPayerPayTypeService payerPayTypeService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private IUserService userService;
    @Autowired
    private IChannelAccountGroupService channelAccountGroupService;
    @RequiresPermissions("channel:channelAccount:view")
    @GetMapping()
    public String channelAccount(ModelMap mmap) {
        PayerPayType search = new PayerPayType();
        search.setPayerUserName("系统自带");
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(search);
        mmap.put("payerPayTypes", payerPayTypes);
        mmap.put("payUrl", payConfig.getHost()+"/pay/test");
        mmap.put("channels", userService.selectChannelList(new User()));
        mmap.put("groups", getChannelGroup());
        mmap.put("userId", ShiroUtils.getSysUser().getUserId());
        return prefix + "/channelAccount";
    }

    /**
     * 获取码商代理
     * @return
     */
    @GetMapping("/channelUser")
    @ResponseBody
    public AjaxResult getSelfPayType(){
        return AjaxResult.success("success", userService.selectChannelList(new User()));
    }

    /**
     * 获取银行信息
     * @return
     */
    @GetMapping("/banks")
    @ResponseBody
    public AjaxResult getBank(){
        List<JSONObject> banks = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","中国农业银行");
        jsonObject.put("phone","95599");
        jsonObject.put("short","ABC");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","中国建设银行");
        jsonObject.put("phone","95533");
        jsonObject.put("short","CCB");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","中国光大银行");
        jsonObject.put("phone","95595");
        jsonObject.put("short","CEB");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","中国民生银行");
        jsonObject.put("phone","95568");
        jsonObject.put("short","CMBC");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","招商银行");
        jsonObject.put("phone","95555");
        jsonObject.put("short","CMB");
        banks.add(jsonObject);


        jsonObject = new JSONObject();
        jsonObject.put("name","中国工商银行");
        jsonObject.put("phone","95588");
        jsonObject.put("short","ICBC");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","中国银行");
        jsonObject.put("phone","95566");
        jsonObject.put("short","BOC");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","中国邮政储蓄银行");
        jsonObject.put("phone","95880");
        jsonObject.put("short","PSBC");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","江苏农信");
        jsonObject.put("phone","1069800096008");
        jsonObject.put("short","JSNX");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","贵州农信");
        jsonObject.put("phone","106980196688");
        jsonObject.put("short","GZNX");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","贵州农信");
        jsonObject.put("phone","106980196688");
        jsonObject.put("short","GZNX");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","贵州农信2");
        jsonObject.put("phone","106571260596688");
        jsonObject.put("short","GZNX2");
        banks.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("name","贵州农信3");
        jsonObject.put("phone","10628841");
        jsonObject.put("short","GZNX3");
        banks.add(jsonObject);
        return AjaxResult.success("success", banks);
    }

    /**
     * 获取银行信息
     * @return
     */
    @GetMapping("/location")
    @ResponseBody
    public AjaxResult getLocation(){
        ArrayList<String> locations = new ArrayList<>();
        locations.add("广东 深圳");
        locations.add("上海 上海");
        locations.add("广东 广州");
        locations.add("广东 东莞");
        locations.add("北京 北京");
        locations.add("广东 佛山");
        locations.add("广西 南宁");
        locations.add("江苏 南京");
        return AjaxResult.success("success", locations);
    }
    /**
     * 获取系统自带支付类型
     * @return
     */
    @GetMapping("/channelGroup")
    @ResponseBody
    public AjaxResult getChannelInfo(){
        return AjaxResult.success("success",getChannelGroup());
    }

    private List<ChannelAccountGroup> getChannelGroup(){
        ChannelAccountGroup channelAccountGroup = new ChannelAccountGroup();
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        List<ChannelAccountGroup> list = new ArrayList<>();
        if (roleKey.equals("channel")) {
            channelAccountGroup.setChannelId(user.getUserId());
            list = channelAccountGroupService.selectChannelAccountGroupList(channelAccountGroup);
        } else if (roleKey.equals("admin") || roleKey.equals("admin2")) {
            list = channelAccountGroupService.selectChannelAccountGroupList(channelAccountGroup);
        }

        return  list;
    }
    /**
     * 查询通道账户列表
     */
    @RequiresPermissions("channel:channelAccount:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChannelAccount channelAccount) {
        startPage();
        // 取身份信息
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        List<ChannelAccount> list = new ArrayList<>();
        if (roleKey.equals("channel")) {
            channelAccount.setChannelUserId(user.getUserId());
            list = channelAccountService.selectChannelAccountList(channelAccount);
        } else if (roleKey.equals("admin") || roleKey.equals("admin2")) {
            list = channelAccountService.selectChannelAccountList(channelAccount);
        }
        return getDataTable(list);
    }


    /**
     * 导出通道账户列表
     */
    @RequiresPermissions("channel:channelAccount:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelAccount channelAccount) {
        // 取身份信息
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        List<ChannelAccount> list = new ArrayList<>();
        if (roleKey.equals("channel")) {
            channelAccount.setChannelUserId(user.getUserId());
            list = channelAccountService.selectChannelAccountList(channelAccount);
        } else if (roleKey.equals("admin") || roleKey.equals("admin2")) {
            list = channelAccountService.selectChannelAccountList(channelAccount);
        }
        ExcelUtil<ChannelAccount> util = new ExcelUtil<ChannelAccount>(ChannelAccount.class);
        return util.exportExcel(list, "channelAccount");
    }

    /**
     * 新增通道账户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        PayerPayType search = new PayerPayType();
        search.setPayerUserName("系统自带");
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(search);
        mmap.put("payerPayTypes", payerPayTypes);
        BusinessInfo businessInfo = new BusinessInfo();
        mmap.put("businessList", businessInfoService.selectBusinessInfoList(businessInfo));
        mmap.put("groups", getChannelGroup());
        return prefix + "/add";
    }

    /**
     * 新增保存通道账户
     */
    @RequiresPermissions("channel:channelAccount:add")
    @Log(title = "通道账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChannelAccount channelAccount) {
        channelAccount.setChannelUserId(ShiroUtils.getUserId());
        return toAjax(channelAccountService.insertChannelAccount(channelAccount));
    }

    /**
     * 修改通道账户
     */
    @GetMapping("/edit/{accountId}")
    public String edit(@PathVariable("accountId") String accountId, ModelMap mmap) {
        ChannelAccount channelAccount = channelAccountService.selectChannelAccountById(accountId);
        mmap.put("channelAccount", channelAccount);
        BusinessInfo businessInfo = new BusinessInfo();
        mmap.put("businessList", businessInfoService.selectBusinessInfoList(businessInfo));
        PayerPayType search = new PayerPayType();
        search.setPayerUserName("系统自带");
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(search);
        mmap.put("payerPayTypes", payerPayTypes);
        mmap.put("groups", getChannelGroup());
        return prefix + "/edit";
    }

    /**
     * 修改保存通道账户
     */
    @RequiresPermissions("channel:channelAccount:edit")
    @Log(title = "通道账户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelAccount channelAccount) {
        return toAjax(channelAccountService.updateChannelAccount(channelAccount));
    }

    /**
     * 删除通道账户
     */
    @RequiresPermissions("channel:channelAccount:remove")
    @Log(title = "通道账户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(channelAccountService.deleteChannelAccountByIds(ids));
    }


    /**
     * 校验email邮箱
     */
    @PostMapping("/checkAccountUnique")
    @ResponseBody
    public String checkAccountUnique(String account,String payType) {
        boolean b = channelAccountService.checkAccountUnique(account, payType);
        if (b != true) {
            return "1";
        }
        return "0";
    }
}
